#include "eutranCtrl.h"
#include "NmmToX86Protocol.h"
#include "syscore.h"
#include "apis.h"
#include <stdint.h>
#include <pthread.h>
#include <errno.h>
#ifdef ignore
#undef ignore
#endif
#include <sstream>
#include <stdlib.h>
#include <algorithm>

uint8_t EarfcnCapacity::bandTable_[65536];
const uint32_t EarfcnCapacity::BAND9;
const uint32_t EarfcnCapacity::BAND38;
const uint32_t EarfcnCapacity::BAND39;
const uint32_t EarfcnCapacity::BAND40;

EarfcnCapacity::EarfcnCapacity(uint32_t mask):
capacities_(mask)
{
    memset(bandTable_, 0xff, sizeof(bandTable_));
    
    for(uint32_t i=0; i<=0xffff; i++)
    {
        if( i>=0 && i<=599 )
        {
            bandTable_[i] = 1;
        }
        else if( i>=600 && i<=1199 )
        {
            bandTable_[i] = 2;
        }
        else if( i>=1200 && i<=1949 )
        {
            bandTable_[i] = 3;
        }
        else if( i>=1950 && i<=2399 )
        {
            bandTable_[i] = 4;
        }
        else if( i>=2400 && i<=2649 )
        {
            bandTable_[i] = 5;
        }
        else if( i>=2650 && i<=2749 )
        {
            bandTable_[i] = 6;
        }
        else if( i>=2750 && i<=3449 )
        {
            bandTable_[i] = 7;
        }
        else if( i>=3450 && i<=3799 )
        {
            bandTable_[i] = 8;
        } 
        else if( i>=3800 && i<=4149 )
        {
            bandTable_[i] = 9;
        }
        else if( i>=4150 && i<=4749 )
        {
            bandTable_[i] = 10;
        }
        else if( i>=4750 && i<=4949 )
        {
            bandTable_[i] = 11;
        }
        else if( i>=5010 && i<=5179 )
        {
            bandTable_[i] = 12;
        }
        else if( i>=5180 && i<=5279 )
        {
            bandTable_[i] = 13;
        }
        else if( i>=5280 && i<=5379 )
        {
            bandTable_[i] = 14;
        }
        else if( i>=5730 && i<=5849 )
        {
            bandTable_[i] = 17;
        }
        else if( i>=5850 && i<=5999 )
        {
            bandTable_[i] = 18;
        }
        else if( i>=6000 && i<=6149 )
        {
            bandTable_[i] = 19;
        }
        else if( i>=6150 && i<=6449 )
        {
            bandTable_[i] = 20;
        }
        else if( i>=6450 && i<=6599 )
        {
            bandTable_[i] = 21;
        }
        else if( i>=6600 && i<=7399 )
        {
            bandTable_[i] = 22;
        }
        else if( i>=7500 && i<=7699 )
        {
            bandTable_[i] = 23;
        }
        else if( i>=7700 && i<=8039 )
        {
            bandTable_[i] = 24;
        }
        else if( i>=8040 && i<=8689 )
        {
            bandTable_[i] = 25;
        }
        else if( i>=8690 && i<=9039 )
        {
            bandTable_[i] = 26;
        }
        else if( i>=9040 && i<=9209 )
        {
            bandTable_[i] = 27;
        }
        else if( i>=9210 && i<=9659 )
        {
            bandTable_[i] = 28;
        }
        else if( i>=9660 && i<=9769 )
        {
            bandTable_[i] = 29;
        }
        else if( i>=9770 && i<=9869 )
        {
            bandTable_[i] = 30;
        }
        else if( i>=9870 && i<=9919 )
        {
            bandTable_[i] = 31;
        }
        else if( i>=9920 && i<=10359 )
        {
            bandTable_[i] = 32;
        }
        else if( i>=36000 && i<=36199 )
        {
            bandTable_[i] = 33;
        }
        else if( i>=36200 && i<=36349 )
        {
            bandTable_[i] = 34;
        }
        else if( i>=36350 && i<=36949 )
        {
            bandTable_[i] = 35;
        }
        else if( i>=36950 && i<=37549 )
        {
            bandTable_[i] = 36;
        }
        else if( i>=37550 && i<=37749 )
        {
            bandTable_[i] = 37;
        }
        else if( i>=37750 && i<=38249 )
        {
            bandTable_[i] = 38;
        }
        else if( i>=38250 && i<=38649 )
        {
            bandTable_[i] = 39;
        }
        else if( i>=38650 && i<=39649 )
        {
            bandTable_[i] = 40;
        }
        else if( i>=39650 && i<=41589 )
        {
            bandTable_[i] = 41;
        }
        else if( i>=41590 && i<=43589 )
        {
            bandTable_[i] = 42;
        }
        else if( i>=43590 && i<=45589 )
        {
            bandTable_[i] = 43;
        }
        else if( i>=45590 && i<=46589 )
        {
            bandTable_[i] = 44;
        }
    }
}

bool EarfcnCapacity::isBandSupported(uint32_t mask)
{
    return (capacities_ & mask)!=0;
}

bool EarfcnCapacity::getEarfcnRangeFromBand(uint8_t band, uint16_t& lowerArfcn, uint16_t& upperArfcn)
{
    if( band==9 )
    {
        lowerArfcn = 3800;
        upperArfcn = 4149;
    }
    else if( band==38 )
    {
        lowerArfcn = 37750;
        upperArfcn = 38249;
    }
    else if( band==39 )
    {
        lowerArfcn = 38250;
        upperArfcn = 38649;
    }
    else if( band==40 )
    {
        lowerArfcn = 38650;
        upperArfcn = 39649;
    }
    else if( band==41 )
    {
        lowerArfcn = 39650;
        upperArfcn = 41589;
    }
    else
    {
        return false;
    }
    return true;
}
